<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 个人信息 -->
    <input type="file" ref="file" hidden @change="changefile">
    <van-cell class="avatar-cell" title="头像" is-link center @click="$refs.file.click()" >
      <van-image
        class="avatar"
        round
        fit="cover"
        :src="getUserList.photo"
        
      />
    </van-cell>
    <van-cell title="昵称" @click="updateNameShow=true" :value="getUserList.name" is-link />
    <van-cell title="性别" @click="updateGenderShow=true" :value="getUserList.gender?'女':'男'" is-link />
    <van-cell title="生日"  @click="updatebirthdayShow=true" :value="getUserList.birthday" is-link />
    <!-- /个人信息 -->
    <!-- 编辑昵称 -->
    <van-popup v-model="updateNameShow" position="bottom" style="height:100%">
      <update-name v-if="updateNameShow" @close="updateNameShow=false" v-model="getUserList.name"></update-name>
    </van-popup>
    <!-- 编辑性别 -->
    <van-popup v-model="updateGenderShow" position="bottom" style="height:50%">
      <update-gender v-if="updateGenderShow" @close="updateGenderShow=false" v-model="getUserList.gender"></update-gender>
    </van-popup>
    <!-- 编辑生日 -->
    <van-popup v-model="updatebirthdayShow" position="bottom" style="height:50%">
      <update-birthday v-if="updatebirthdayShow" @close="updatebirthdayShow=false" :birthday.sync="getUserList.birthday">></update-birthday>
    </van-popup>
    <!-- 编辑头像 -->
    <van-popup v-model="updatephotoShow" position="bottom" style="height:100%">
     <update-photo v-if="updatephotoShow" @updata-photo="getUserList.photo=$event" @close="updatephotoShow=false" :img="showImgUrl"></update-photo>
    </van-popup>
  </div>
</template>

<script>
import {getUserProfile} from '@/api/user'
import updateName from "./components/update-name.vue"
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'
export default {
  name: 'UserProfile',
  components: {
    updateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  props: {},
  data () {
    return {
      getUserList:[],
      updateNameShow:false,
      updateGenderShow:false,
      updatebirthdayShow:false,
      updatephotoShow:false,
      showImgUrl:''
    }
  },
  created () {
    this.getUserProfile()
  },
  methods: {
   async getUserProfile(){
     try{
     let {data} = await getUserProfile()
     console.log(data.data);
     this.getUserList=data.data
     }catch(error){
       return this.$toast.fail('获取用户资料失败')
     }
   },
   changefile(){
    //  文件信息
     const file = this.$refs.file.files[0]
     console.log(file);
    // //  类型判断
     const type = ['image/png','image/gif','image/jpg','image/jpeg']
     if(!type.includes(file.type)){
       this.$refs.file.value=''
       return this.$toast.fail('图片类型不正确')
     }
    // //  大小判断
     if(file.size / 1024 / 1024 >3){
       this.$refs.file.value=''
       return this.$toast.fail('文件大小不能超过3M')
     }
    // //  清空内容
    this.showImgUrl=URL.createObjectURL(file)
     this.$refs.file.value=''
     this.updatephotoShow=true
   }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
    }
    .avatar {
      width: 60px;
      height: 60px;
    }
  }
  .van-popup{
      background-color:#f5f7f9;
  }  
}
</style>
